﻿Public Class frmStaffType

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        ClearText()
    End Sub
    Private Sub ClearText()
        Me.txtID.Text = ""
        Me.txtName.Text = ""
        Me.txtNote.Text = ""
        Me.txtName.Focus()
        Me.txtID.Text = AutoID4Access("SELECT StaffTypeID FROM tbl_stafftypes", "tbl_stafftypes") + 1
        LoadDataToLVStaffType("SELECT * FROM tbl_stafftypes", LV)

        Me.btbUpdate.Enabled = False
        Me.btnSave.Enabled = True
    End Sub
    Private Sub frmStaffType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Connect()
        Me.txtID.Text = AutoID4Access("SELECT StaffTypeID FROM tbl_stafftypes", "tbl_stafftypes") + 1
        LoadDataToLVStaffType("SELECT * FROM tbl_stafftypes", LV)
        Me.txtID.Enabled = False
        Me.btbUpdate.Enabled = False
        Me.btnSave.Enabled = True
        Me.txtName.Focus()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (Me.txtID.Text = "") Then
            MsgBox("Please Enter Value !", MsgBoxStyle.Exclamation, "Invalid Value")
            Me.txtID.Focus()
        ElseIf (Me.txtName.Text = "") Then
            MsgBox("Please Enter Value!", MsgBoxStyle.Exclamation, "Invalid Value")
            Me.txtName.Focus()
        Else
            Dim SQL As String = "INSERT INTO tbl_stafftypes(StaffTypeID,StaffType,StaffNote)" & _
                              "VALUES (" & Me.txtID.Text & ",'" & Me.txtName.Text & "','" & txtNote.Text & "')"
            SaveDataToAccess(SQL)
            ClearText()
            LoadDataToLVStaffType("SELECT * FROM tbl_stafftypes", LV)
            Me.txtID.Text = AutoID4Access("SELECT StaffTypeID FROM tbl_stafftypes", "tbl_stafftypes") + 1
        End If
    End Sub

    Private Sub LV_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LV.MouseDoubleClick
        Dim i As Integer
        For i = 0 To Me.LV.Items.Count - 1
            If LV.Items(i).Selected = True Then
                txtID.Text = LV.Items(i).Text
                Me.txtName.Text = LV.Items(i).SubItems(1).Text
                Me.txtNote.Text = LV.Items(i).SubItems(2).Text
            End If
        Next
        Me.btbUpdate.Enabled = True
        Me.btnSave.Enabled = False
    End Sub

    Private Sub LV_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LV.SelectedIndexChanged

    End Sub

    Private Sub btbUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbUpdate.Click
        Dim SQL As String = "UPDATE tbl_stafftypes SET StaffType='" & Me.txtName.Text & _
                                           "' ,StaffNote='" & txtNote.Text & "' WHERE StaffTypeID=" & Me.txtID.Text

        SaveDataToAccess(SQL)
        LoadDataToLVPositions("SELECT * FROM tbl_positions", LV)
        ClearText()
    End Sub
End Class